package com.yoda.mall.mysql.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.HomeBrandApi;
import com.yoda.mall.model.entity.sms.HomeBrand;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.mysql.mapper.HomeBrandMapper;
import com.yoda.mall.mysql.mapper.HomeNewProductMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author shawn
 * @date 2022年 12月 16日 22:23
 */
@DubboService
public class HomeBrandApiImpl implements HomeBrandApi {

    @Resource
    private HomeBrandMapper homeBrandMapper;

    @Resource
    private HomeNewProductMapper homeNewProductMapper;

    @Override
    public PageResult<HomeBrand> selectByPage(Integer page, Integer pageSize, String brandName, Integer recommendStatus) {
        Page<HomeBrand> brandPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<HomeBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(brandName),HomeBrand::getBrandName,brandName);
        wrapper.eq(Objects.nonNull(recommendStatus),HomeBrand::getRecommendStatus,recommendStatus);
        wrapper.orderByDesc(HomeBrand::getSort);
        homeBrandMapper.selectPage(brandPage, wrapper);
        return new PageResult<>(page, pageSize, (int) brandPage.getTotal(), brandPage.getRecords());

    }

    @Override
    public Integer createRecommendBrand(List<HomeBrand> homeBrands) {
      return homeBrandMapper.saveBatch(homeBrands);
    }

    @Override
    public void updateHomeBrandById(List<Long> ids,Integer recommendStatus) {
        homeBrandMapper.updateBatchById(ids,recommendStatus);
    }

    @Override
    public void changeBrandSort(Long id, Integer sort) {
        HomeBrand homeBrand = new HomeBrand();
        homeBrand.setId(id);
        homeBrand.setSort(sort);

        homeBrandMapper.updateById(homeBrand);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        homeBrandMapper.deleteBatchIds(ids);
    }



}
